package cn.com.surker.workcon.service.bcs;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.bcs.common.ProductFlagEnum;
import cn.com.surker.workcon.service.ProductService;
import cn.com.surker.enums.production.invoice.ControlProStatusEnum;
import cn.com.surker.control.entity.ControlInvoice;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中青林工控机的生产数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProductServiceImpl extends GKJDBCService implements ProductService {

	/**
	 * 根据erp的派单信息,查询派车数据
	 *
	 * @param dispatchId erp的派单id
	 * @return
	 */
	public ProductionIndentDispatch queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String selectSql = "select top 1 ErpID,ProdID,prodMete,morMete,pieCnt,totVehs,totMete,prodTag,prodTimE,recipe,Flag from product where erpId = " + dispatchId;
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(selectSql);
		if (maps == null || maps.isEmpty()) {
			return null;
		}
		Map<String, Object> map = maps.get(0);
		String flag = String.valueOf(map.get("Flag"));
		Double prodMete = Double.valueOf(String.valueOf(map.get("prodMete")));
		String prodId = String.valueOf(map.get("prodId"));
		String prodTimE = String.valueOf(map.get("prodTimE"));
		Date prodTim = DateUtil.convertDate(prodTimE);
		String recipe = String.valueOf(map.get("recipe"));
		//	设置类型
		Integer controlProStatus = null;
		//	取出最后一个字段
		String bFlage = flag.substring(flag.length() - 1);
		bFlage = bFlage.toLowerCase();
		if (ProductFlagEnum.AUTOMATIC.getCode().equals(bFlage)) {
			//	自动完成,处理发货单自动完成的情况
			controlProStatus = ControlProStatusEnum.AUTOMATIC.getCode();
		} else if (ProductFlagEnum.MOVEMENT.getCode().equals(bFlage)) {
			//	手动生产,处理发货单手动生产的情况
			controlProStatus = ControlProStatusEnum.MOVEMENT.getCode();
		} else if (ProductFlagEnum.SEMI_AUTOMATIC.getCode().equals(bFlage)) {
			//	半自动生产完成,处理发货单半自动生产完成的情况
			controlProStatus = ControlProStatusEnum.SEMI_AUTOMATIC.getCode();
		} else if (ProductFlagEnum.INTERRUPT.getCode().equals(bFlage)) {
			//	生产中断完成,处理发货单生产中断完成的情况
			controlProStatus = ControlProStatusEnum.INTERRUPT.getCode();
		} else if (ProductFlagEnum.COMPLETED.getCode().equals(bFlage)) {
			//	整车数据已完整
			controlProStatus = ControlProStatusEnum.COMPLETED.getCode();
		}
		ProductionIndentDispatch pid = new ProductionIndentDispatch();
		pid.setProTime(prodTim);
		pid.setProStere(prodMete);
		pid.setControlId(prodId);
		pid.setControlProStatus(controlProStatus);
		pid.setRecipe(recipe);
		return pid;
	}

	@Override
	public Boolean inProing(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询sql
		String querySql = "select ID from Producing where ErpID = " + dispatchId + " and Station = " + line.getControlLineId();
		//
		List<Map<String, Object>> maps = lineJdbcTemp.queryForList(querySql);
		if (maps == null || maps.isEmpty()) {
			//	没有数据
			return false;
		}
		return true;
	}

	@Override
	public List<ControlInvoice> findFullByMin(Long lineId, String lineName, Date minDate, Long minProdId) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询数据
		String querySql = "select erpId,id prodId,code indentCode,ProjName conName,Customer clientName,ProjAdr address,ConsPos site,BetLev strengthName,Lands slumps,pour castModel,MorMete mortarStere,ProdMete concreteStere,Vehicle carCode,TotVehs totalCar,operator mixer,\n" +
			"prodTimE proTime,ProdID controlInvoiceNo,CASE WHEN erpid = 0 THEN 1 ELSE 0 END conAuto,? lineId,? lineName\n" +
			"from Product where station = " + line.getControlLineId() + "\n";
		if (minProdId != null) {
			querySql += "and id > " + minProdId;
		} else if (minDate != null) {
			querySql += "and prodTimE > '" + DateUtil.convertString(minDate, "yyyy-MM-dd HH:mm:ss") + "'";
		}
		querySql += " order by prodTimE;";
		Object[] params = {lineId, lineName};
		List<ControlInvoice> weightSpotChecks = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return weightSpotChecks;
	}

	@Override
	public List<ControlInvoice> findByTime(Long lineId, String lineName, String startTime, String endTime) {
		if (lineId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询数据
		String querySql = "select erpId,id prodId,code indentCode,ProjName conName,Customer clientName,ProjAdr address,ConsPos site,BetLev strengthName,Lands slumps,pour castModel,MorMete mortarStere,ProdMete concreteStere,Vehicle carCode,TotVehs totalCar,operator mixer,\n" +
			"prodTimE proTime,ProdID controlInvoiceNo,CASE WHEN erpid = 0 THEN 1 ELSE 0 END conAuto,? lineId,? lineName\n" +
			"from Product where station = ? and prodTimE >= ? and prodTimE < ? order by prodTimE;\n";
		Object[] params = {lineId, lineName, line.getControlLineId(), startTime, endTime};
		List<ControlInvoice> controlInvoices = lineJdbcTemp.query(querySql, params, BeanPropertyRowMapper.newInstance(ControlInvoice.class));
		return controlInvoices;
	}
}
